package cn.jekun.web.agent.action;

import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.map.HashedMap;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.core.Constants;
import cn.jekun.component.core.bean.gen.RsAgent;
import cn.jekun.component.core.bean.gen.RsUserAgent;
import cn.jekun.component.core.service.gen.RsUserAgentService;
import cn.jekun.web.agent.bean.UserAgent;
import cn.jekun.web.agent.service.UserAgentService;
import cn.jekun.web.shop.bean.UserShop;

/**
 * 操作员查询
 * @author twj
 *
 */
@SuppressWarnings("serial")
public class UserAgentAction extends AgentBaseAction{
	private String userAgentId;
	private String realName;
	public String getUserAgentId() {
		return userAgentId;
	}
	public void setUserAgentId(String userAgentId) {
		this.userAgentId = userAgentId;
	}
	public String getRealName() {
		return realName;
	}
	public void setRealName(String realName) {
		this.realName = realName;
	}
	@Resource(name = UserAgentService.BEAN_NAME)
	UserAgentService userAgentService;
	
	
	/**
	 * 查询代理商所有的操作员
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String queryAllByAgentId() {
	    JSONObject jsonObject = new JSONObject();
	    JSONArray jsonArray = new JSONArray();
		//checkRight(AUTH_SHOP_TERNIMAL_USER);
		formMap.put(RsAgent.COL_AGENT_ID, this.getSessionUser().getAgentId());
		formMap.put(UserAgent.USER_AGENT_ID, userAgentId);
		formMap.put(UserAgent.REAL_NAME, realName);
		SqlUtis.prepareSql(formMap, SqlUtis.getSQL(formMap,
				"a." + RsAgent.COL_AGENT_ID, Types.VARCHAR, SqlUtis.EQ),
				SqlUtis.getSQL(formMap,"a."+UserAgent.USER_AGENT_ID,Types.VARCHAR,SqlUtis.EQ),
				SqlUtis.getSQL(formMap,"b."+UserAgent.REAL_NAME,Types.VARCHAR,SqlUtis.LIKE));
		try{
//		this.resultList = this.userAgentService.list(formMap, page);
		this.resultList = this.userAgentService.getUserAgentList(this.getSessionUser().getAgentId(),userAgentId,realName,page);
		for(int i = 0,length = resultList.size(); i<length; i++){
        	UserAgent userAgent = (UserAgent)resultList.get(i);
            HashMap map = new HashMap();
            map.put("realName", userAgent.getRealName()==null?"":userAgent.getRealName()); //用户真实名
            map.put("userName", userAgent.getUserName()==null?"":userAgent.getUserName()); //用户登录名
            map.put("userAgentId", userAgent.getUserAgentId()==null?"":userAgent.getUserAgentId()); //操作员代码
            jsonArray.add(map);
        }
        jsonObject.element("resultList",jsonArray);
    	jsonObject.element(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
		jsonObject.element(ErrorMsg.KEY_MESSAGE, "成功调用");
		jsonObject.put(Constants.Page.PAGE_NUMBER,page.getPageNumber());
		jsonObject.put(Constants.Page.PAGE_SIZE,page.getPageSize());
		jsonObject.put(Constants.Page.TOTAL_COUNT,page.getTotalCount());
		jsonObject.put(Constants.Page.TOTAL_PAGE,page.getPages());
		System.out.println("JSONOBJECT="+jsonObject);
		}catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
		}
        return this.writeJson(jsonObject);
//		return "/agent/userAgentList.jsp";
	}
	
	
//	/**
//	 * 修改备注名
//	 * @return
//	 */
//	@SuppressWarnings({ "rawtypes", "unchecked" })
//	public String editMemo(){
//		JSONObject jsonObject=new JSONObject();
//		try{
//			String memo = request.getParameter("memo");
//			this.userAgentService.editMemo(userAgentId, memo);
//			
//			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
//	
//			jsonObject.put(ErrorMsg.KEY_MESSAGE,"修改成功");
//		}catch(Exception e){
//			
//			e.printStackTrace();
//			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//
//			jsonObject.put(ErrorMsg.KEY_MESSAGE,"修改失败");
//				
//			}
//		
//		return this.writeJson(jsonObject);
//	}

}
